﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

using WoodRobotics.PasswordKeeper.ModelView;
using Google.GData.Spreadsheets;


namespace WoodRobotics.PasswordKeeper.ViewModel
{

    public class Passwords : INotifyPropertyChanged
    {
        
        #region Constructor 
        public Passwords()
        {
            PasswordList = new ObservableCollection<Password>();
            m_GConnection = new GoogleConnection();
        }
        #endregion

        #region Fields
        private GoogleConnection m_GConnection;
        #endregion
        
        #region Properties
        public ObservableCollection<Password> PasswordList { get; private set; }
        public string ConnectionUserName
        {
            get { return m_GConnection.UserName; }
            set
            {
                m_GConnection.UserName = value;
                NotifyPropertyChanged("ConnectionUserName");
            }
        }
        public string ConnectionPassword
        {
            get { return m_GConnection.Password; }
            set
            {
                m_GConnection.Password = value;
                NotifyPropertyChanged("ConnectionPassword");
            }
        }
        public string EncryptPassword
        {
            get { return EncryptiongConverter.Password; }
            set 
            { 
                EncryptiongConverter.Password = value;
                if (EncryptUpdated != null)
                    EncryptUpdated(this, null);
            }
        }
        public bool IsConnected
        {
            get { return m_GConnection.IsConnected; }
            set
            {
                m_GConnection.IsConnected = value; 
                NotifyPropertyChanged("IsConnected");
            }
        }
        #endregion
        
        #region Events
        public event EventHandler EncryptUpdated;
        
        private void PasswordList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
                ((Password)e.NewItems[0]).Row = m_GConnection.AddRow();
            else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
                ((Password)e.OldItems[0]).Row.Delete();
        }
        #endregion
        
        #region Methods
        public void Connect()
        {
            
            m_GConnection.Connect();
            
            NotifyPropertyChanged("IsConnected");
            
            if (m_GConnection.IsConnected)
            {
                
                foreach (ListEntry l_Ent in m_GConnection.GetPasswords())
                    PasswordList.Add(new Password() { Row = l_Ent });
                
                PasswordList.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(PasswordList_CollectionChanged);
                
            }
            
        }
        public void Disconnect()
        {
            
            m_GConnection.IsConnected = false;
            NotifyPropertyChanged("IsConnected");
            
            PasswordList.CollectionChanged -= new System.Collections.Specialized.NotifyCollectionChangedEventHandler(PasswordList_CollectionChanged);
            
            PasswordList.Clear();
            
        }
        private void NotifyPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
        #endregion
        
        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
        
    }
    
}
